﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Serialization;
namespace EEE
{
    public class Program
    {
        static void Main(string[] args)
        {
            OrderService a = new OrderService();
            a.addOrder(0001, "A", "甲", 10);
            a.addOrder(0002, "B", "乙", 8);
            a.addOrder(0003, "C", "丙", 15);
            a.addOrder(0004, "D", "丁", 13);
            
            a.Export();
            a.Import();
            a.display();
            a.addOrder(0005, "F", "戊", 34);        
            Console.WriteLine();
            a.display();

            a.deleteOrder(a.searchByItem("A"));      
            Console.WriteLine();
            a.display();

            a.deleteOrder(a.searchByClient("己"));      
            Console.WriteLine();
            a.display();
        }
    }
    public class Order
    {
        public int ID;         
        public int orderID
        {
            get { return ID; }
            set
            {
                if (value >= 0) { ID = value; }
                else { ID = 0; }
            }
        }
        public OrderDetails od = new OrderDetails();

        public override bool Equals(object obj)
        {
            Order a = obj as Order;
            return this.ID == a.ID;
        }
        public override int GetHashCode()
        {
            return ID;
        }
    }
    public class OrderDetails
    {
        public int price;
        public Client user = new Client();
        public OrderItem Item = new OrderItem();
        public int Price
        {
            get { return price; }
            set
            {
                if (value >= 0) { price = value; }
                else { price = 0; }
            }
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return Convert.ToString(price);
        }

    }
    [Serializable]
    public class OrderService
    {
        public List<Order> ListOrder = new List<Order>();
        public void addOrder(int ID, string Item, string Client, int price)
        {       
            Order o = new Order();
            o.orderID = ID;
            o.od.Item.itemName = Item;
            o.od.user.clientName = Client;
            o.od.Price = price;
            bool same = false;
            foreach (Order n in ListOrder)
            {
                if (n.Equals(o))
                    same = true;
            }
            if (!same)
                ListOrder.Add(o);
        }
        public void deleteOrder(List<Order> ListDelete)
        {
            if (ListOrder.Except(ListDelete).Count() < 1 || ListDelete.Count() == 0)
            {
                System.Console.WriteLine("订单删除失败");
                return;
            }
            ListOrder = ListOrder.Except(ListDelete).ToList();
        }

        public void rchangeOrder(List<Order> Listchange, int ID, string Itemname, string ClientN, int PriceN)
        {
            if (ListOrder.Except(Listchange).Count() < 1 || Listchange.Count() == 0)
            {
                System.Console.WriteLine("订单修改失败");
                return;
            }
            ListOrder = ListOrder.Except(Listchange).ToList();
            foreach (Order n in Listchange)
            {
                n.orderID = ID;
                n.od.Item.itemName = Itemname;
                n.od.user.clientName = ClientN;
                n.od.Price = PriceN;
            }
            ListOrder = ListOrder.Union(Listchange).ToList();
        }

        public List<Order> searchByID(int id)         
        {
            return ListOrder.Where(n => n.orderID == id).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByItem(string name)            
        {
            return ListOrder.Where(n => n.od.Item.itemName == name).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByClient(string client)           
        {
            return ListOrder.Where(n => n.od.user.clientName.Equals(client)).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public List<Order> searchByPrice(int price)        
        {
            return ListOrder.Where(n => n.od.Price == price).ToList().OrderBy(n => n.od.Price).ToList();
        }

        public void sort()                                               
        {
            ListOrder = ListOrder.OrderBy(n => n.orderID).ToList();
        }

        public void display()                                               
        {
            foreach (Order n in ListOrder)
            {
                Console.WriteLine($"订单编号：{n.orderID.ToString()} 客户：{n.od.user.ToString()} 订单金额：{n.od.ToString()} 商品名称：{n.od.Item.ToString()}");
            }
        }

        public void Export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            string XmlFileName = "order.xml";
            FileStream fileStream = new FileStream(XmlFileName, FileMode.Create);
            xmlSerializer.Serialize(fileStream, ListOrder);
            fileStream.Close();
        }

        public void Import()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            string XmlFileName = "order.xml";
            FileStream fileStream = new FileStream(XmlFileName, FileMode.Open);
            ListOrder =
                xmlSerializer.Deserialize(fileStream) as List<Order>;
            fileStream.Close();
            string xml = File.ReadAllText(XmlFileName);
            Console.WriteLine(xml);

        }
    }
    public class Client
    {
        public string client;
        public string clientName
        {
            get { return client; }
            set
            {
                client = value;
            }
        }

        public override string ToString()
        {
            return client;
        }
    }
    public class OrderItem
    {
        public string item;
        public string itemName
        {
            get { return item; }
            set { item = value; }
        }
        public override string ToString()
        {
            return item;
        }
    }
}
